// pages/memorize/memorizeProcessOn/memorizeProcessOn.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dialogWord: null,
        wrong:false,
        wordsNums:0,
        cardWord:null,
        cardQuestion:[null,null,null,null],
        show: 5, //0单词卡片, 1记忆英语卡片, 2记忆翻译卡片, 3完成页
        questionList:[], //问题列表
        tableID:0,
        hasRememberWordsNum:0, // 已经记忆单词数量
        willRememberWordsNum:0 // 将要记忆单词数量
    },
  
   
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            tableID:options.table,
            hasRememberWordsNum:options.hasRememberWordsNum,
            willRememberWordsNum:options.willRememberWordsNum
        })
       
        wx.request({
            url: 'http://39.101.199.5:8888/SuJiWords/enWords/rememberWords',
            method: 'POST',
            data:{
              "table":this.data.tableID,
              "hasRememberWordsNum":this.data.hasRememberWordsNum,
              "willRememberWordsNum":this.data.willRememberWordsNum
            },
            header:{
              'content-tupe':'application/json'
            },
            success: res =>{
            var position =parseInt( Math.random()*4);
            var questions = [null,null,null,null]
            questions[position]=res.data.data[0].answer;
            for(var i=0,z=0;i<4;i++){
                if(questions[i]!=null){
                    continue
                }else{
                    questions[i]=res.data.data[0].wrongEnwords[z]
                    z++
                }
            }
            
            this.setData({
                questionList : res.data.data,
                cardQuestion:questions,
                cardWord:res.data.data[0].answer,
                cardTranslation:res.data.data[0].answer.translation
            })
            this.showToMemorize(this.data.questionList[this.data.wordsNums].answer)
            }
          })
          
    },
    // 加载下一个单词
    toNextWords(e){
        ++this.data.wordsNums;  
        if(this.data.questionList.length>this.data.wordsNums){
         this.setData({
             wordsNums:this.data.wordsNums,
             show:0,
             cardWord:this.data.questionList[this.data.wordsNums].answer
         })
        }else {
         this.data.wordsNums =0
            console.log("这是else"+this.data.questionList[0])
         this.showToEnQuestion(this.data.questionList[this.data.wordsNums])
        }
     },
    //  加载下一个考察英语问题
     toNextEnlish(e){
         var choose = e.detail.result;
         if(choose){
         this.data.wordsNums++;
         if(this.data.questionList.length>this.data.wordsNums){
         this.showToEnQuestion(this.data.questionList[this.data.wordsNums])
         console.log("现在是下一个单词")
         }else{
             this.data.wordsNums =0
            console.log("这是else"+this.data.questionList[0])
         this.showToTranslation(this.data.questionList[this.data.wordsNums])
         }}else{ 
            console.log(this.data.questionList[this.data.wordsNums].answer) 
            this.setData({
             dialogWord:this.data.questionList[this.data.wordsNums].answer,
             wrong:true
         })}
     },
    //  加载下一个考察翻译问题
    toNextTranslation(e){
        var choose = e.detail.result;
        if(choose){
        this.data.wordsNums++;
        if(this.data.questionList.length>this.data.wordsNums){
        this.showToTranslation(this.data.questionList[this.data.wordsNums]);
         console.log("现在是下一个翻译"+this.data.wordsNums);
        }else{
           this.setData({
               show:3
           })
           this.finishMemorize()
        //    添加完成记忆代码
        }}
        else{
            console.log(this.data.questionList[this.data.wordsNums].answer) 
            this.setData({
             dialogWord:this.data.questionList[this.data.wordsNums].answer,
             wrong:true
         })
        }
    },
    showToEnQuestion(Question){
        var position =parseInt( Math.random()*4);
        var questions = [null,null,null,null]
        questions[position]=Question.answer;
        for(var i=0,z=0;i<4;i++){
            if(questions[i]!=null){
                continue
            }else{
                questions[i]=Question.wrongEnwords[z]
                z++
            }
        }
        this.setData({
            cardQuestion:questions,
            show:1
        })
    },
    showToTranslation(Question){
        var position =parseInt( Math.random()*4);
        var questions = [null,null,null,null]
        questions[position]=Question.answer;
        for(var i=0,z=0;i<4;i++){
            if(questions[i]!=null){
                continue
            }else{
                questions[i]=Question.wrongEnwords[z]
                z++
            }
        }
        this.setData({
            cardQuestion:questions,
            show:2
        })
    },
    changeType(){
        if(this.data.wrong)this.setData({wrong:false})
        else this.setData({wrong:true})
    },
    showToMemorize(MemorizeWord){
        this.setData({
            cardWord:MemorizeWord,
            show:0
        })
    },
    finishMemorize(){
        var app = getApp();
        wx.request({
        url: 'http://39.101.199.5:8888/SuJiWords/MemoriesNumber/updateMemorize',
        method:"POST",
        data:{
        "phone":app.globalData.loginPhoneNumber,
        "tableNo":this.data.tableID,
        "memorizeNum":this.data.willRememberWordsNum
        },
        header:{
            'content-tupe':'application/json'
          },
          success: res =>{
            console.log(res)
            setTimeout(function(){
                wx.switchTab({
                  url: '../../memorize/memorize',
                  
                })
            },2000)
        }
        })
        
    }
})